<?php

namespace App\Http\Controllers\Client\Course;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CourseDetailController extends ClientController
{

    /**
     * 课程介绍
     */
    public function index()
    {

        $all = $this->all;
        if(!isset($all['course_id'])){
            return $this->setReturnJson(-1,'未传参数');
        }

        try {
            // 验证是否必须购买课程
            $need_to_buy = $this->needToBuy($all['course_id']);
            if ($need_to_buy) {
                return $this->setReturnJson(-1, '您尚未购买该课程');
            }

            // 查询课程资源
            $source = $this->courseSources($all['course_id']);

            // 查询课程章节目录
            $contents = $this->courseContents($all['course_id']);

            return $this->setReturnJson(0, 'success', ['source' => $source, 'contents' => $contents]);
        } catch (\Exception $e) {
            Log::error(CourseDetailController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '课程查询失败');
        }

    }

    /**
     * 验证用户是否需要购买课程
     * @param int $course_id
     * @return bool
     */
    public function needToBuy(int $course_id): bool
    {
        // 查询课程价格，如果免费直接放行
        $price = DB::table('laifei_sys_course')->where('course_id','=',$course_id)->value('price');
        if($price == 0){
            return false;
        }

        // 查询用户是否购买了课程,未购买返回true
        $bought = DB::table('laifei_course')
            ->where('course_id','=',$course_id)
            ->where('we_id','=',$this->we_id)
            ->count();
        return $bought? false: true;
    }

    /**
     * 查询课程资源
     * @param int $course_id
     * @return array
     */
    public function courseSources(int $course_id):array
    {
        $result = DB::table('laifei_sys_course')
            ->where('course_id','=',$course_id)
            ->first();
        $result = json_decode(json_encode($result),true);
        $result['title_img'] = AliOss::getUrl($result['title_img']);
        $result['back_img'] = AliOss::getUrl($result['back_img']);
        return $result;
    }

    /**
     * 课程章节资源
     * @param int $course_id
     * @return array
     */
    public function courseContents(int $course_id):array
    {
        // 查询课程信息
        $contents = DB::table('laifei_sys_lessons')
            ->where('course_id','=',$course_id)
            ->get();

        // 查询学习记录
        $lesson_ids = $contents->pluck('lesson_id')->toArray();
        $learned_lesson_ids = DB::table('laifei_lessons')
            ->where('we_id','=',$this->we_id)
            ->whereIn('lesson_id',$lesson_ids)
            ->pluck('lesson_id')
            ->toArray();

        $contents = $contents->map(function($item) use ($learned_lesson_ids){
            // 计算时间
            $minute = (string)floor($item->duration/60);
            $seconds = (string)$item->duration%60;
            $minute = strlen($minute) === 1? '0'.$minute: $minute;
            $seconds = strlen($seconds) === 1? '0'.$seconds: $seconds;
            $item->duration = $minute . ':' . $seconds;
            $item->url = AliOss::getUrl($item->url);
            if(in_array($item->lesson_id,$learned_lesson_ids)){ // 如果已学习过，添加记录
                $item->learned = 1;
            }
            return $item;
        })
            ->toArray();

        return $contents;
    }



}